#include "LyvmLayoutLibrary.h"
#include <assert.h>

const int ID_SHRINK_STATIC_LAYOUT = 40448;   /* Example */
const int ID_STRETCH_STATIC_LAYOUT = 40449;   /* Example */
const int ID_GRAPHVIZ_STATIC_LAYOUT= 40450;	// layout using Graphviz, add by ParrotT, 2010/04/21

const int ID_SPRING_DYNAMIC_LAYOUT = 50558;   /* Example */
const int ID_BINARYTREE_DYNAMIC_LAYOUT = 50559;/*add by hyt*/
const int ID_LINKLIST_DYNAMIC_LAYOUT = 50560; /*ass by hyt*/

LyvmLayoutLibrary* LyvmLayoutLibrary::_lLib = NULL;

LyvmLayoutLibrary::LyvmLayoutLibrary(void){
	_slyTable[ID_SHRINK_STATIC_LAYOUT] = new Shrink_Static_lyvm();   /* Example */
	_slyTable[ID_STRETCH_STATIC_LAYOUT] = new Stretch_Static_lyvm();   /* Example */
	_slyTable[ID_GRAPHVIZ_STATIC_LAYOUT]= new Graphviz_Static_lyvm();	// layout using Graphviz, add by ParrotT, 2010/04/21
	
	_dlyTable[ID_SPRING_DYNAMIC_LAYOUT] = new Spring_Dynamic_lyvm();   /* Example */
	_dlyTable[ID_BINARYTREE_DYNAMIC_LAYOUT] = new BinaryTree_Dynamic_Lyvm();
	_dlyTable[ID_LINKLIST_DYNAMIC_LAYOUT] = new LinkList_Dynamic_Lyvm();
}

/// singleton method 
LyvmLayoutLibrary* LyvmLayoutLibrary::getInstance(){
	if (_lLib == NULL) 
		_lLib = new LyvmLayoutLibrary();
	return _lLib;
}

Basic_Static_lyvm* LyvmLayoutLibrary::getStaticLyVMbyID(int ID){
	map<int,Basic_Static_lyvm*>::iterator pos ;
	for(pos = _slyTable.begin(); pos!=_slyTable.end(); pos++) {
		if(pos->first == ID)
			return pos->second;
	}
	return NULL;
}

Basic_Dynamic_lyvm* LyvmLayoutLibrary::getDynamicLyVMbyID(int ID){
	map<int,Basic_Dynamic_lyvm*>::iterator pos ;
	for(pos = _dlyTable.begin(); pos!=_dlyTable.end(); pos++) {
		if(pos->first == ID)
			return pos->second;
	}
	return NULL;
}


map<int ,string> LyvmLayoutLibrary::getStaticIDNameList(){
	map<int,string> idNameList;
	map<int,Basic_Static_lyvm*>::iterator p;

	for(p = _slyTable.begin(); p!=_slyTable.end(); p++) {
		string name = getStaticLyVMbyID(p->first)->getName();
		idNameList[p->first] = name;
	}

	return idNameList;
}

map<int ,string> LyvmLayoutLibrary::getDynamicIDNameList(){
	map<int,string> idNameList;
	map<int,Basic_Dynamic_lyvm*>::iterator q;

	for(q = _dlyTable.begin(); q!=_dlyTable.end(); q++) {
		string name = getDynamicLyVMbyID(q->first)->getName();
		idNameList[q->first] = name;
	}

	return idNameList;
}

Basic_Dynamic_lyvm* LyvmLayoutLibrary::getCurrentEnabledDynamicLy(){
	map <int,string> dly_id_name_list;
	map <int,string>::iterator itr;
	Basic_Dynamic_lyvm* lyvm = NULL;
	dly_id_name_list = LyvmLayoutLibrary::getInstance()->getDynamicIDNameList();
	for(itr=dly_id_name_list.begin();itr!=dly_id_name_list.end();itr++){		
		lyvm = LyvmLayoutLibrary::getInstance()->getDynamicLyVMbyID(itr->first);
		if(lyvm->getFlag()){
			break;
		}
	}
	return lyvm;
}

bool LyvmLayoutLibrary::doesLayoutEnable(){
	map <int,string> dly_id_name_list;
	map <int,string>::iterator itr;
	Basic_Dynamic_lyvm* lyvm;
	dly_id_name_list = LyvmLayoutLibrary::getInstance()->getDynamicIDNameList();
	for(itr=dly_id_name_list.begin();itr!=dly_id_name_list.end();itr++){		
		lyvm = LyvmLayoutLibrary::getInstance()->getDynamicLyVMbyID(itr->first);
		if(lyvm->getFlag()){
			return true;
		}
	}
	return false;
}
